package com.iris.live.services.data.repositories.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.iris.live.services.data.models.VirusKillerModel;
import com.iris.live.services.data.repositories.SystemRepository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.models.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author DF
 */
@Repository
public class SystemRepositoryImpl extends BaseRepository implements SystemRepository {
    private static Logger logger = LoggerFactory.getLogger(SystemRepositoryImpl.class);

    @Override
    public PageData getMessageList(Integer pageSize, Integer pageIndex, String receiver, String sender,
                                   String updateDate, String content, String remark, String permit) {
        StringBuilder sql = new StringBuilder();
        sql.append(
                " select * from (  SELECT " +
                        "	b.`经销商名` as 发送展厅,a.is_read as 状态, " +
                        "	IF(a.发送人='LiVE系统','LiVE系统',d.`人员姓名`) as 发送人, " +
                        "	c.`经销商名` as 接收展厅, " +
                        "	e.`人员姓名` as 接收人, " +
                        "	a.`消息分类`, " +
                        "	a.`消息内容`, " +
                        "	a.`消息备注`, " +
                        "	a.`更新时间` as 发送时间," +
                        "   a.`电话号码` " +
                        "FROM " +
                        "	`LSH消息` a, " +
                        "	`LSH经销商表` b, " +
                        "	`LSH经销商表` c, " +
                        "	`用户` d, " +
                        "	`用户` e " +
                        "WHERE a.`发送许可` = b.`许可_BMBS` " +
                        "AND a.`接收许可` = c.`许可_BMBS` " +
                        "AND a.`发送人` = d.`用户名` " +
                        "AND a.接收许可=:permit " +
                        "AND a.`接收人` = e.`用户名` " +
                        "AND a.`是否删除` = '否' "
        );
        Map<String, String> params = Maps.newHashMap();
        params.put("permit", permit);
        if (!Strings.isNullOrEmpty(receiver)) {
            sql.append("AND a.`接收人` = :receiver ");
            params.put("receiver", receiver);
        }
        if (!Strings.isNullOrEmpty(sender)) {
            sql.append("AND d.`人员姓名` LIKE :sender ");
            params.put("sender", "%" + sender + "%");
        }
        if (!Strings.isNullOrEmpty(updateDate)) {
            sql.append("AND a.`更新时间` LIKE :updateDate ");
            params.put("updateDate", "%" + updateDate + "%");
        }
        if (!Strings.isNullOrEmpty(content)) {
            sql.append("AND a.`消息内容` LIKE :content ");
            params.put("content", "%" + content + "%");
        }
        if (!Strings.isNullOrEmpty(remark)) {
            sql.append("AND a.`消息备注` LIKE :remark ");
            params.put("remark", "%" + remark + "%");
        }
        sql.append("ORDER BY a.更新时间 desc   ) a  ORDER BY 状态   ");
        return getPage(sql.toString(), params, pageSize, pageIndex);
    }

    @Override
    public ResultData getMessageListModelAndIsDelete(String username, String permit) {
        StringBuilder sql = new StringBuilder();
        sql.append(
                "                    SELECT  " +
                        "                     b.`经销商名` as 发送展厅,  " +
                        "                     IF(a.发送人='LiVE系统','LiVE系统',d.`人员姓名`) as 发送人,  " +
                        "                     c.`经销商名` as 接收展厅,    " +
                        "                     e.`人员姓名` as 接收人,   " +
                        "                     a.`消息分类`, a.`消息编号` ,  " +
                        "                     a.`消息内容`,   " +
                        "                      a.`消息备注`, a.`是否删除`,   " +
                        "                      a.`更新时间` as 发送时间  " +
                        "                        FROM  " +
                        "                       `LSH消息` a  " +
                        "                       LEFT JOIN  `LSH经销商表` b on a.`发送许可` = b.`许可_BMBS` " +
                        "                       LEFT JOIN  `LSH经销商表` c  on a.`接收许可` = c.`许可_BMBS`  " +
                        "                       LEFT JOIN  `用户` d on a.`发送人` = d.`用户名`   " +
                        "                       LEFT JOIN    `用户` e on a.`接收人` = e.`用户名`   " +
                        "                        WHERE  a.接收人=:username " +
                        "     AND a.接收许可=:permit        " +
                        "  and a.`是否删除` = '否'  "
        );
        Map<String, String> params = Maps.newHashMap();
        params.put("username", username);
        params.put("permit", permit);
        sql.append("ORDER BY a.更新时间 desc");
        ResultData reda = new ResultData();
        try {

            PageData item = getPage(sql.toString(), params, 15, 0);

            reda.setMessage("查询有数据");
            reda.setResult("true");
            reda.setPageData(item);
        } catch (Exception e) {
            logger.error("运行异常。", e);
            reda.setMessage("查询没有数据");
            reda.setResult("false");
            reda.setPageData(null);
        }

        return reda;


    }



    @Override
    public List getMessageSettingList(String permit, String type) {
        StringBuilder sb = new StringBuilder();
        sb.append(
                "SELECT   " +
                        "   a.id,  " +
                        "	a.type,  " +
                        "	b.`所属部门`,  " +
                        "	b.`职位`,  " +
                        "   b.角色,   " +
                        "	b.`用户名`,  " +
                        "	b.`人员姓名`,  " +
                        "	b.`人员电话`,  " +
                        "	a.`create_date`,  " +
                        "	a.`modify_date`,  " +
                        "	a.`remark`,  " +
                        "	a.`checked`,  " +
                        "	a.permit  " +
                        "FROM message_setting AS a  " +
                        "LEFT JOIN `用户` AS b ON b.`用户名` = a.`user`   " +
                        "WHERE a.type = :type  " +
                        "AND permit = :permit  "
        );
        Map<String, String> params = Maps.newHashMap();
        params.put("permit", permit);
        params.put("type", type);
        return getList(sb.toString(), params);
    }

    @Override
    public List<VirusKillerModel> getVirusKillerList() {
        final String queryString = "select model  from VirusKillerModel model";

        return super.entities.createQuery(queryString).getResultList();
    }
}
